﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.TrainingUI
{
    public partial class AssignTrainee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateTrainings();
            }
        }

        private void PopulateAssignedTrainee()
        {
            int trainingId = Convert.ToInt32(selectTraining.Value);
            TrainingManager manager=new TrainingManager();
            List<ReportTrainedEmployee>assignTrainees=new List<ReportTrainedEmployee>();
            assignTrainees = manager.GetAllTrainedEmployee(trainingId);
            msgTotalTrainee.Text ="Total "+assignTrainees.Count.ToString()+ " Trainees under this Training session";
            assignedEmployeesGdv.DataSource = assignTrainees;
            assignedEmployeesGdv.DataBind();
        }

        private void PopulateTrainings()
        {
            TrainingManager manager = new TrainingManager();
            List<Training> trainings = new List<Training>();
            trainings = manager.GetAllTrainings();
            selectTraining.DataValueField = "Id";
            selectTraining.DataTextField = "Name";
            selectTraining.DataSource = trainings;
            selectTraining.DataBind();
            selectTraining.Items.Insert(0, "--Select Training--");
        }

        protected void AssignEmployee(object o, EventArgs e)
        {
            try
            {
                TrainedEmployee trainee = new TrainedEmployee();
                Code code = new Code();
                Employee employee = new Employee();
                code.CodeName = txtEmpCode.Value;
                employee.EmpCode = code;
                trainee.Employee = employee;
                Training training = new Training();
                training.Id = Convert.ToInt32(selectTraining.Value);
                trainee.Training = training;
                TrainingManager manager=new TrainingManager();
                if(manager.AssignEmployeeToTraining(trainee))
                {
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "Employee Successfully Assigned to Training";
                    
                }
                else
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "Employee Already Assigned to the Training";
                }
                PopulateAssignedTrainee();
            }
            catch (Exception ex)
            {
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = ex.Message;
            }

        }
    }
}